using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace DataStructure
{
    //只具备头指针，存储键值对
    public class LinkedList3<Key,Value>
    {
        private class Node
        {
            public Key key;
            public Value value;
            public Node next;
            
            public Node(Key key, Value value,Node next)
            {
                this.key = key;
                this.value = value;
                this.next = next;
            }
            
            
            public override string ToString()
            {
                return key.ToString() + " : " + value.ToString();
            }
        }
        
        private Node head;
        private int size;
        
        public LinkedList3()
        {
            head = null;
            size = 0;
        }
        
        public int Count => size;
        public bool IsEmpty => size == 0;
        
        //私有辅助寒素，通过键查找对应的结点
        private Node GetNode(Key key)
        {
            Node cur = head;
            while(cur != null)
            {
                if (cur.key.Equals(key))
                    return cur; 
                    
                cur = cur.next;
            }
            return null;
        }
        
        public void Add(Key key,Value value)
        {
            Node node = GetNode(key);
            
            //如果结点不存在，则创建
            if (node == null)
            {
                head = new Node(key, value, head);
                size++;
            }
            else//若存在，则更新
                node.value = value;
        }
        
        //查看链表中是否包含指定键
        public bool Contains(Key key)
        {
            return GetNode(key) != null;
        }
        
        //通过键获取值
        public Value Get(Key key)
        {
            Node node = GetNode(key);
            
            if(node == null)
                throw new ArgumentException("键" + key + "不存在");
            else
                return node.value;
        }
        
        //将键对应的值改成一个新的值
        public void Set(Key key,Value newValue)
        {
            Node node = GetNode(key);
            if (node == null)
                throw new ArgumentException("键" + key + "不存在");
            else
                node.value = newValue;
        }
        
        //将键值对删除
        public void Remove(Key key)
        {
            if (head == null)
                return;
            if (head.key.Equals(key))
            {
                head = head.next;
                size--;
            }
            else
            {   
                Node cur = head;
                Node pre = null;
                
                while (cur != null)
                {
                    if (cur.key.Equals(key))
                    {
                        break;
                    }
                    pre = cur;
                    cur = cur.next;
                }
                if (cur != null)
                {
                    pre.next = pre.next.next;
                    size--;
                }
            }
        }
    }
}